package com.sinodata.bsm.center.engine.event;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.center.cache.EventRuleCache;
import com.sinodata.bsm.center.cache.NewEventCache;
import com.sinodata.bsm.center.service.event.EventService;
import com.sinodata.bsm.center.util.spring.SpringContextHolder;
import com.sinodata.bsm.common.constants.EventConstants;
import com.sinodata.bsm.common.vo.Event;
import com.sinodata.bsm.common.vo.EventRule;

/**
 * 
 * <p>
 * Description: 事件自动清除处理线程
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-5-29 AM 10:23:52    liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class EventAutoConfirmCenter extends Thread {

    private static Logger logger = Logger.getLogger(EventAutoConfirmCenter.class);

    private static EventAutoConfirmCenter instance = null;

    private static NewEventCache newEventCache = SpringContextHolder.getBean(NewEventCache.class);

    private static EventRuleCache eventRuleCache = SpringContextHolder.getBean(EventRuleCache.class);

    private static EventService eventService = SpringContextHolder.getBean(EventService.class);

    public EventAutoConfirmCenter() {
        logger.info("[Tip]>>>>>Start Event Auto Confirm.");
    }

    public static EventAutoConfirmCenter getInstance() {
        if (instance == null) {
            instance = new EventAutoConfirmCenter();
        }
        return instance;
    }

    @Override
    public void run() {
        while (true) {
            List<Event> newEvent = newEventCache.getAllNewEvents();
            for (Event event : newEvent) {
                try {
                    Long resId = event.getResId();
                    List<EventRule> eventRules = eventRuleCache.getAvailableAutoConfirmRule(resId, event.getEventTypeId(), event.getLevel());
                    if (eventRules == null || eventRules.size() == 0) {
                        continue;
                    }
                    //多规则，符合任何一个规则即自动清除
                    //TODO 判断事件的有效时间范围
                    for (EventRule eventRule : eventRules) {
                        if (eventRule != null) {
                            //防止自动清除规则清除掉
                            if (eventRule.getConfirmDuration() == null || eventRule.getConfirmDuration() <= 0) {
                                continue;
                            }
                            //end
                            long time = System.currentTimeMillis();
                            Date occurTime = event.getOccurTime();
                            if ((time - occurTime.getTime()) >= eventRule.getConfirmDuration() * 1000) {
                                eventService.confirm(event.getId(), EventConstants.CONFIRM_TYPE_AUTO, EventConstants.REMARKER_SYSTEM, EventConstants.REMARKER_SYSTEM, "系统自动确认", true);
                            }
                        }
                    }
                } catch (Exception e) {
                    logger.error("event auto confirm error", e);
                }
            }
            //考虑批量自动确认掉
            try {
                Thread.sleep(1 * 60 * 1000L);
            } catch (InterruptedException e) {
                logger.error("InterruptedException  when call " + this.getClass().getName() + "run()", e);
                continue;
            }
        }
    }
}
